package com.ruoyi.project.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.disk.domain.DiskFile;
import com.ruoyi.disk.service.IDiskFileService;
import com.ruoyi.project.domain.Project;
import com.ruoyi.project.mapper.ProjectMapper;
import com.ruoyi.project.service.IProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 项目信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-11
 */
@Service
@Slf4j
public class ProjectServiceImpl implements IProjectService 
{
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private IDiskFileService diskFileService;

    /**
     * 查询项目信息
     * 
     * @param id 项目信息主键
     * @return 项目信息
     */
    @Override
    public Project selectProjectById(Long id)
    {
        return projectMapper.selectProjectById(id);
    }

    /**
     * 查询项目信息列表
     * 
     * @param project 项目信息
     * @return 项目信息
     */
    @Override
    //@DataScope(deptAlias = "d")
    public List<Project> selectProjectList(Project project)
    {
        return projectMapper.selectProjectList(project);
    }

    /**
     * 新增项目信息
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertProject(Project project)
    {
        project.setCreateTime(DateUtils.getNowDate());
        project.setUpdateTime(DateUtils.getNowDate());
        int rows = projectMapper.insertProject(project);
        if(rows>0){
            //初始化项目文件夹
            diskFileService.initUserFile(project.getProjectName(),project.getUserId(),project.getId());
        }
        return rows;
    }

    //初始化用户文件夹
    /**
     * 修改项目信息
     * 
     * @param project 项目信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProject(Project project)
    {
        Project project1 =  projectMapper.selectProjectById(project.getId());
        if(Objects.isNull(project1)){
            throw new ServiceException("项目不存在");
        }
        DiskFile diskFile = new DiskFile();
        diskFile.setProjectId(project.getId());
        List<DiskFile> list = diskFileService.selectDiskFileList(diskFile);
        if(!CollectionUtils.isEmpty( list)){
            list.forEach(x->{
                x.setName(project.getProjectName());
                x.setOldName(x.getName());
                diskFileService.updateDiskFile(x);
            });
        }
        project.setUpdateTime(DateUtils.getNowDate());
        return projectMapper.updateProject(project);
    }

    /**
     * 批量删除项目信息
     * 
     * @param ids 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteProjectByIds(Long[] ids) {
        //校验项目是否可以删除
        if (ids.length > 0) {
            for (Long id : ids) {
                Project project = projectMapper.selectProjectById(id);
                if (Objects.isNull(project)) {
                    throw new ServiceException("删除项目不存在");
                }
                List<DiskFile> diskFiles =  diskFileService.getAllFileByParentName(project.getProjectName());
                diskFiles.stream().forEach(x->{
                    if(x.getIsDir()!=1){
                        throw new ServiceException("项目下存在文件，无法删除");
                    }
                });
                Long[] idsFile= diskFiles.stream()
                        .map(DiskFile::getId)   // 提取每个DiskFile对象的id
                        .toArray(Long[]::new);
                if(idsFile.length>0){
                    diskFileService.removeByParentIds(idsFile);
                }

            }

            return projectMapper.deleteProjectByIds(ids);
        }
        return 0;
    }
    /**
     * 删除项目信息信息
     * 
     * @param id 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id)
    {
        return projectMapper.deleteProjectById(id);
    }
}
